/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.khstudio.api.dialog;

import android.app.DialogFragment;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.khstudio.videocapture.R;

public class KhDialogProgress extends DialogFragment {
    private ProgressBar mProgress;
    private TextView    mPercent;
    private TextView    mCount;
    private Button      mCancel;

    private String  mStrTitle;

    private int mMaxCnt;
    private int count;
    private int progress;

    private KhFileDialogListener mListener;

    public interface KhFileDialogListener {
        void onClickCancel();
    }

    public static KhDialogProgress show(FragmentManager fm, String title, int maxCnt, KhFileDialogListener l) {
        FragmentTransaction ft = fm.beginTransaction();
        Fragment prev = fm.findFragmentByTag("khdialog");

        if (prev != null) {
            ft.remove(prev);
        }

        Bundle args = new Bundle();
        if(title != null) {
            args.putString("title", title);
        }
        args.putInt("maxcnt", maxCnt);

        // Create and show the dialog.
        KhDialogProgress f = new KhDialogProgress();
        f.setOnListener(l);
        f.setArguments(args);
        f.show(ft, "khdialog");

        return f;
    }

    public void setOnListener(KhFileDialogListener l) {
        mListener = l;
    }

    public void setCount(int cnt) {
        if(this.count != cnt) {
            _setCount(cnt);
        }
    }

    public void setProgress(int progress) {
        if(this.progress != progress) {
            _setProgress(progress);
        }
    }

    private KhDialogProgress() {
        mProgress   = null;
        mPercent    = null;
        mCount      = null;
        mCancel     = null;

        mListener   = null;

        mStrTitle   = null;

        mMaxCnt     = 0;
        count       = 0;
        progress    = 0;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mStrTitle = getArguments().getString("title", null);
        mMaxCnt = getArguments().getInt("maxcnt");

        if(mStrTitle != null) {
            setStyle(DialogFragment.STYLE_NORMAL, 0);
        } else {
            setStyle(DialogFragment.STYLE_NO_TITLE, 0);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = null;

        if(mStrTitle != null) {
            getDialog().setTitle(mStrTitle);
        }
        getDialog().setCanceledOnTouchOutside(false);

        v = _createView(inflater, container, savedInstanceState);

        return v;
    }

    private View _createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.kh_dialog_progress, container, false);

        mProgress = (ProgressBar) v.findViewById(R.id.kh_dialog_progress_pb);
        mPercent = (TextView) v.findViewById(R.id.kh_dialog_progress_percent);
        _setProgress(0);

        mCount = (TextView) v.findViewById(R.id.kh_dialog_progress_count);
        _setCount(0);

        mCancel = (Button) v.findViewById(R.id.kh_dialog_progress_cancel);
        mCancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mListener != null) {
                    mListener.onClickCancel();
                } else {
                    dismiss();
                }
            }
        });

        return v;
    }

    private void _setProgress(int progress) {
        this.progress = progress;
        mProgress.setProgress(progress);
        mPercent.setText(String.format("%d %%", progress));
    }

    private void _setCount(int cnt) {
        this.count = cnt;
        mCount.setText(String.format("%d / %d", cnt, mMaxCnt));
    }
}
